package accueil.pack;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class Scroll extends View implements View.OnTouchListener {

    String[] liste;
    String value;
    int valueId;
    TextView textView1,textView2,textView3;
    
    float x, oldX;
    
    public Scroll(Context context, AttributeSet attrs,TextView textView1,TextView textView2,TextView textView3, String[] liste)
    {
        super(context, attrs);
        this.liste = liste;
        this.textView1= textView1;
        this.textView2= textView2;
        this.textView3= textView3;
        valueId = (int)liste.length/2;
        
        float x = 0, oldX=0;
        
        
        changeValue("");
        
        setFocusable(true);
	setFocusableInTouchMode(true);
	this.setOnTouchListener(this);
        

    }
    
    public void changeValue( String mouvement)
    {
        String texte1="",texte2="",texte3="";
        
        Log.i(" mouvement","mouvement =" + mouvement);
        
        if ((mouvement.equals("reculer"))&&(valueId != 0))
        {
            valueId--;
        }
        if ((mouvement.equals("avancer"))&&(valueId != liste.length-1))
        {
            valueId++;
        }
       
        texte2= liste[valueId];
        if(valueId == 0)
        {
            texte1 = "";
            
        }
        else
        {
            texte1= liste[valueId-1];
            if(texte1.length()>3)
            {
                texte1=texte1.substring(texte1.length()-3,texte1.length());
            }
        }
            
        if (valueId == liste.length-1)
        {
            texte3 = "";
        }
        else
        {
            texte3= liste[valueId+1];
            if(texte3.length()>3)
            {
                texte3=texte3.substring(0,3);
            }
        }
       

        textView1.setText(texte1);
        textView2.setText(texte2);
        textView3.setText(texte3);
        
    }
    
    public boolean onTouch(View view, MotionEvent event) {
        
        
        
        if ((event.getActionMasked () == event.ACTION_DOWN)||(event.getActionMasked () == event.ACTION_UP))
        {
           oldX = event.getX(0);
           x = event.getX(0);
        }
        
        
        if (event.getActionMasked () == event.ACTION_MOVE)
        {
           x = event.getX(0);
        }
        
        if ((int)((x-oldX)/20) > 1)
        {
            changeValue("reculer");
            oldX=x;
        }
        if ((int)((x-oldX)/20) < -1)
        {
            changeValue("avancer");
            oldX=x;
        }
        
        
        
        
        return true;
    }


}
